package com.twu.biblioteca;

import java.util.Scanner;

/**
 * Created by phazel on 13/04/14.
 */
public class MainMenu
{
    Library lib = new Library();
    Scanner kb;
    boolean loggedIn = false;
    UserAuthenticator auth;
    User currentUser;

    public void start()
    {
        kb = new Scanner(System.in);

        while(true)
        {
            printMenuItems();

            System.out.print("\nEnter selection: ");
            String input = kb.nextLine();
            char selection = input.charAt( 0 );

            if( selection == 'q' || selection == 'Q' )
            {
                System.out.println( "Quitting Biblioteca" );
                break;
            }

            if( loggedIn )
            {
                handleMenuSelectionLoggedIn( selection );
            }
            else
            {
                handleMenuSelectionLoggedOut( selection );
            }
        }


        kb.close();
    }

    private void printMenuItems()
    {
        System.out.println("\n***** Main Menu *****\n");
        if( loggedIn )
        {
            System.out.println("a. List Books");
            System.out.println("b. Checkout a book");
            System.out.println("c. Return a book");
            System.out.println("d. List Movies");
            System.out.println("e. Book Borrower Printout");
        }
        else
        {
            System.out.println("a. List Books");
            System.out.println("b. List Movies");
            System.out.println("c. Book Borrower Printout");
            System.out.println("d. Login");
        }
    }

    private void handleMenuSelectionLoggedIn( char selection )
    {
        try
        {
            if( selection == 'a' )
            {
                listBooks();
            }
            else if( selection == 'b')
            {
                checkoutBook();
            }
            else if( selection == 'c' )
            {
                returnBook();
            }
            else if( selection == 'd' )
            {
                listMovies();
            }
            else if( selection == 'e' )
            {
                bookBorrowerPrintout();
            }
            else
            {
                System.out.println("Select a valid option!");
            }
        }
        catch(Exception e)
        {
            System.out.println("Select a valid option!");
        }

    }

    private void handleMenuSelectionLoggedOut( char selection )
    {
        try
        {
            if( selection == 'a' )
            {
                listBooks();
            }
            else if( selection == 'b' )
            {
                listMovies();
            }
            else if( selection == 'c' )
            {
                bookBorrowerPrintout();
            }
            else if( selection == 'd' )
            {
                loginUser();
            }
            else
            {
                System.out.println("Select a valid option!");
            }
        }
        catch(Exception e)
        {
            System.out.println("Select a valid option!");
        }

    }

    private void listBooks()
    {
        lib.listBooks();
    }

    private void checkoutBook()
    {
        lib.checkoutBook( currentUser );
    }

    private void returnBook()
    {
        lib.returnBook();
    }

    private void listMovies()
    {
        lib.listMovies();
    }

    private void loginUser()
    {
        auth = new UserAuthenticator( kb ); // Uuughh I hate this, would love to find another way
        auth.login();
        currentUser = auth.getCurrentUser();
        if( currentUser != null )
        {
            loggedIn = true;
        }
    }

    private void bookBorrowerPrintout()
    {
        lib.listCheckedOutBooks();
    }
}
